package ink.tiptop.utils;

import ink.tiptop.utils.date.DateBaseHolder;
import ink.tiptop.utils.date.DateHolder;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ink.tiptop.zjf
 * @Title: Ticket
 * @Package ink.tiptop.utils
 * @Description:
 * @date 2020-07-19 11:03
 */
public class ParamUtils {

    public static boolean isIp(String ipStr) {
        if (ipStr != null && !ipStr.isEmpty()) {
            // 定义正则表达式
            String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." +
                    "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +
                    "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +
                    "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)" +
                    ":([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-5]{2}[0-3][0-5])$";
            // 判断ip地址是否与正则表达式匹配
            if (ipStr.matches(regex)) {
                // 返回判断信息
                return true;
            } else {
                // 返回判断信息
                return false;
            }
        }
        return false;
    }

    public static String dateToStrPattern(Date date, DateBaseHolder.Pattern pattern) {
        if (date == null) {
            return "-";
        }
        return DateHolder.formDate(date).toString(pattern);
    }

    public static boolean isAnyBlank(String...objects) {
        for (String obj : objects) {
            if (obj == null) {
                return true;
            }
            if (StringUtils.isBlank(obj)) {
                return true;
            }
        }
        return false;
    }

    public static List<Integer> toIntList(List<String> id) {
        return id.parallelStream().map(it-> Integer.parseInt(it)).collect(Collectors.toList());
    }

    /**
     路径限制
     * @param pathRemote
     * @param i
     * @return
     */
    public static boolean pathLimit(String pathRemote, int i) {
        pathRemote = pathRemote.replaceAll("\\\\", "/");
        String[] paths = pathRemote.split("/");
        if (paths.length > i) {
            return true;
        }
        return false;
    }
}
